<template>
  <div id='tmpl'>
      <!--提交评论功能-->
       <div id="postcomment">
            <h3>提交评论</h3>
            <p class="p"></p>
            <textarea placeholder="请输入你要评论的内容" v-model="content"></textarea>
            <mt-button type="primary" size="large" @click="postcomment">发表</mt-button>
       </div>

       <!--获取评论功能-->
       <div id="list">
            <h3>评论列表</h3>
            <p class="p"></p>
            <div v-for="(item,index) in list" :key="index">
                <div class="title" >
                  <span>{{index+1}}</span>
                  <span>用户：{{item.user_name}}</span>
                  <span>发布时间：{{item.add_time | datefmt('YYYY-MM-DD HH:mm:ss')}}</span>    
                </div>
                <ul class="mui-table-view">
				           <li class="mui-table-view-cell">{{item.content}}</li>
			          </ul>
            </div>
            <mt-button size="large" type="danger" plain @click="getmore">加载更多</mt-button>
      </div>
  </div>
</template>

<script>
import common from '../../kits/common.js';
import {Toast} from 'mint-ui';
export default {
  name: "component_name",
  props:['id'],
  data () {
    return {
      content:'',
      list:[],
      pageindex:1
    };
  },
  created(){
    this.getcomment(this.pageindex);
  },
  methods:{
    // 提交评论
      postcomment(){
        if( this.content.trim().length <=0 ){
          Toast("输入内容不能为空");
          return ;
        }
        var url = common.apidomain + '/api/postcomment/' + this.id;
        this.$http.post(url,{content:this.content},{emulateJSON:true})
        .then(function(res){
            Toast(res.body.message);

            this.list = [{
              "user_name":"匿名用户",
              "add_time":new Date(),
              "content":this.content
            }].concat(this.list);

            this.content = '';
        })
      },

      // 获取评论
      getcomment(pageindex){
        var pageindex = pageindex || 1;
        var url = common.apidomain + '/api/getcomments/'+this.id+'?pageindex='+pageindex;
        this.$http.get(url).then(function (res) { 
            var data = res.body;
            if(data.status!=0){
                Toast(data.message);
                return;
            }
            this.list.push.apply(this.list,data.message);
         })

      },

      // 加载更多评论
      getmore(){
        this.pageindex++;
        console.log(this.pageindex);
        this.getcomment(this.pageindex);
      }
  }
}
</script>
<style lang="css" scoped>
    #postcomment {
        padding: 5px;
    }
    .p {
        height: 1px;
        width: 100%;
        border-bottom: 1px solid rgba(0, 0, 0, .3);
    }
    #list {
      padding: 5px;
      
    }
    .title {
      padding: 5px;
      font-size: 15px;
      color: #6d6d72;
      background-color: rgba(0, 0, 0, .1);
    }
    .title span {
      padding: 5px;
    }
</style>